package cn.wolfcode.shop.cloud.web.controller;


import cn.wolfcode.shop.cloud.RedisService;

import cn.wolfcode.shop.cloud.util.SecKillKeyPrefix;

import cn.wolfcode.shop.cloud.util.VerifyCodeImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller

public class VerifyCodeController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("initVerifyCode")
    public void initVerifyCode(String uuid, HttpServletResponse response) throws IOException {
        //生成验证码
        String code = VerifyCodeImgUtil.generateVerifyCode();
        //生成结果
        Integer calc = VerifyCodeImgUtil.calc(code);

        //放入到redis中
        redisService.set(SecKillKeyPrefix.SECKILL_VERIFYCODE,uuid,calc);

        BufferedImage verifyCodeImg = VerifyCodeImgUtil.createVerifyCodeImg(code);
        ImageIO.write(verifyCodeImg,"jpg",response.getOutputStream());
    }
}
